package P057_UseRegexpOnComplexSituation;

import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Desc:&emsp;&ensp;推荐在复杂字符串操作环境中使用正则表达式
 *
 * @author 李渊&emsp;liyuan7@yusys.com.cn
 * @version 1.0
 * @since V4.3&emsp;2022/3/24
 */
public class UseRegexpOnComplexSituation {
    private String s1 = "Today is Monday";
    private String s2 = "Today is  Monday";
    private String s3 = "Today is Monday?No!";
    private Pattern pattern = Pattern.compile("\\b\\w+\\b");
    @Test
    public void Test1() {
        statisticWordsBySplit(s1);
        statisticWordsBySplit(s2);
        statisticWordsBySplit(s3);
        statisticWordsByRegexp(s1);
        statisticWordsByRegexp(s2);
        statisticWordsByRegexp(s3);
    }

    public void statisticWordsBySplit(String source){
        System.out.println(source + " 单词数："+source.split(" ").length);
    }
    public void statisticWordsByRegexp(String source){
        Matcher matcher = pattern.matcher(source);
        int count = 0;
        while(matcher.find()){
            count++;
        }
        System.out.println(source + " 单词数："+count);
    }
}
